package component

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedis"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisRpc"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Method"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Uuid"
	"strconv"
	"strings"
)

type gateWayRouteService struct {
}

func (e *gateWayRouteService) BuildId(appName string, appType string) string {
	return appType + ":" + appName
}

func (e *gateWayRouteService) BuildUri(appName string, appType string, port int) string {
	// 构造uri
	uri := "http://localhost:" + strconv.Itoa(port) + "/" + appType + "/" + appName
	if strings.HasSuffix(uri, "-service") {
		uri = uri[0 : len(uri)-len("-service")]
	}
	if strings.HasSuffix(uri, "-native") {
		uri = uri[0 : len(uri)-len("-native")]
	}

	return uri
}

func (e *gateWayRouteService) buildRouter(appName string, appType string, port int) map[string]interface{} {
	shortAppName := appName
	if strings.HasSuffix(strings.ToLower(shortAppName), "-service") {
		shortAppName = shortAppName[0 : len(shortAppName)-len("-service")]
	}
	if strings.HasSuffix(strings.ToLower(shortAppName), "-native") {
		shortAppName = shortAppName[0 : len(shortAppName)-len("-native")]
	}

	predicateArgs := make(map[string]interface{})
	predicateArgs["_genkey_0"] = "/" + appType + "/" + shortAppName + "/**"

	predicate := make(map[string]interface{})
	predicate["name"] = "Path"
	predicate["args"] = predicateArgs

	predicates := make([]map[string]interface{}, 0)
	predicates = append(predicates, predicate)

	// 构造uri
	uri := e.BuildUri(appName, appType, port)

	// 构造过滤器
	filterArgs := make(map[string]interface{})
	filterArgs["_genkey_0"] = "2"

	filter := make(map[string]interface{})
	filter["name"] = "StripPrefix"
	filter["args"] = filterArgs

	filters := make([]map[string]interface{}, 0)
	filters = append(filters, filter)

	body := make(map[string]interface{})
	body["id"] = e.BuildId(appName, appType)
	body["predicates"] = predicates
	body["uri"] = uri
	body["filters"] = filters
	body["order"] = 0
	body["metadata"] = make(map[string]interface{})

	return body
}

func (e *gateWayRouteService) RegisterRouter(appName string, appType string, port int) {
	// 参数检查
	if Method.HasEmpty(appName, appType) {
		return
	}

	body := e.buildRouter(appName, appType, port)

	restFulRequestVO := commRedisRpc.RestFulRequestVO{}
	restFulRequestVO.Uri = "/gateway/route/add"
	restFulRequestVO.Method = "post"
	restFulRequestVO.Data = body
	restFulRequestVO.Uuid = Uuid.RandomUUID()

	jsn := Json.BuildJson(restFulRequestVO)
	if jsn == "" {
		return
	}

	commRedis.Client.Publish("topic_gateway_request", jsn)
}

func (e *gateWayRouteService) UpdateRouter(appName string, appType string, port int) {
	// 参数检查
	if Method.HasEmpty(appName, appType) {
		return
	}

	body := e.buildRouter(appName, appType, port)

	restFulRequestVO := commRedisRpc.RestFulRequestVO{}
	restFulRequestVO.Uri = "/gateway/route/update"
	restFulRequestVO.Method = "post"
	restFulRequestVO.Data = body
	restFulRequestVO.Uuid = Uuid.RandomUUID()

	commRedis.Client.Publish("topic_gateway_request", restFulRequestVO)
}

func (e *gateWayRouteService) UnregisterRouter(id string) {
	// 参数检查
	if Method.HasEmpty(id) {
		return
	}

	body := make(map[string]interface{})

	restFulRequestVO := commRedisRpc.RestFulRequestVO{}
	restFulRequestVO.Uri = "/gateway/route/delete/" + id
	restFulRequestVO.Method = "post"
	restFulRequestVO.Data = body
	restFulRequestVO.Uuid = Uuid.RandomUUID()

	commRedis.Client.Publish("topic_gateway_request", restFulRequestVO)
}
